package cn.sh.ideal.web.view.display.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**  
 * @Description:  视图展示模块ztree查询的公共类  
 * @author: zhangyy   
 * @date:   2018年7月9日 下午9:03:31   
 * @version V1.0  
*/

@Controller
@RequestMapping(value="/get")
@PropertySource(value = { "classpath:jdbc.properties" })
public class QueryViewDisplayZtreeController {
	private RestTemplate rs = new RestTemplate();
	
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	@Value("${server_address}")
	private String serverAddress;
	
	/**
	 *  视图展示模块ztree查询的公共方法
	 * @param  monitorCenterHostInfomationType
	 * @return result
	 * 前端ajax： contentType 默认不设定  request.getParameter可以获取参数
	 * get  方式向monitorCenter项目发出请求 参数以拼接方式传递 	@GetMapping("/getRoles") 接收
	 */
	@RequestMapping(value = "/queryViewDisplayZtree", method = RequestMethod.POST)
	@ResponseBody
	public String queryViewDisplayZtree(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String monitorCenterHostInfomationType=request.getParameter("monitorCenterHostInfomationType");
		StringBuffer url = new StringBuffer(serverAddress);
		url.append("/queryViewDisplayZtree?monitorCenterHostInfomationType=").append(monitorCenterHostInfomationType);

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url.toString(),
				HttpMethod.GET, entity, String.class);

		String result = exchange.getBody();

		return result;
	}
}
